#include "ASTLinksTable.h"
#include "ASTLinkType.h"
#include "ASTLink.h"
#include <string>
#include <iostream>

using namespace std;

ASTLinksTable::ASTLinksTable(void)
{

}

void ASTLinksTable::insertLink(ASTLink* nodeLink)
{
	this->nodeLinks.push_back(nodeLink);
}

void ASTLinksTable::printTable(void)
{
	for(unsigned int i=0;i<nodeLinks.size();++i)
	{
		ASTLink* tmpLink = this->nodeLinks[i];
		cout << "from: " << tmpLink->getFromNode() << " to " << tmpLink->getToNode() << ", linktype: " << tmpLink->getLinkType() << ", index: " << tmpLink->getLinkIndex() << endl;
	}
}

ASTLink* ASTLinksTable::getLink(unsigned int fromNodeIndex, unsigned int linkType)
{
	for (int i=0; i<this->nodeLinks.size();++i)
	{
		ASTLink* tmpLink = this->nodeLinks.at(i);
		if(tmpLink->getFromNode()==fromNodeIndex && tmpLink->getLinkType()==linkType)
		{
			return tmpLink;
		}
	}
	// if there are no results return null OR exception can be thrown NODE_NOT_FOUND
	return nullptr;
}

ASTLink* ASTLinksTable::getChildLink(unsigned int fromNodeIndex, unsigned int childNumber)
{
	for (int i=0; i<this->nodeLinks.size();++i)
	{
		ASTLink* tmpLink = this->nodeLinks.at(i);
		if(tmpLink->getFromNode()==fromNodeIndex && tmpLink->getLinkIndex()==childNumber && tmpLink->getLinkType()==3)
		{
			return tmpLink;
		}
	}
	// if there are no results return null OR exception can be thrown NODE_NOT_FOUND
	return nullptr;
}